﻿using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IntelligentEscalator.DAL;
using IntelligentEscalator.ElevatorWebSocket;
using IntelligentEscalator.Models;

namespace IntelligentEscalator
{
    public class WorkOrderJob : IJob
    {
        public async Task Execute(IJobExecutionContext context)
        {
            MqttDataDAL mqttDataDAL = new MqttDataDAL();
            for (int i = 0; i < 4; i++)
            {
                try
                {
                    string type = string.Empty;
                    int datanum = 0;
                    mqttdata mqttdata = new mqttdata();

                    if (i == 0)
                    {
                        type = "fault";
                        datanum = mqttDataDAL.QueryUntreatedFault();
                    }
                    else if (i == 1)
                    {
                        type = "inspection";
                        datanum = mqttDataDAL.QueryUntreatedInspectionOrder();
                    }
                    else if (i == 2)
                    {
                        type = "repair";
                        datanum = mqttDataDAL.QueryUntreatedRepairOrder();
                    }
                    else if (i == 3)
                    {
                        type = "maintain";
                        datanum = mqttDataDAL.QueryUntreatedMaintainOrder();
                    }
                    mqttdata.dataNum = datanum;
                    mqttdata.manufacturer = "KONE";
                    mqttdata.manufacturerId = "04";
                    mqttdata.topicType = "OrderIncomplete";
                    mqttdata.dataType = type;
                    bool result = mqttDataDAL.CheckData(mqttdata.manufacturer, mqttdata.manufacturerId, type, mqttdata.topicType);

                    if (result)
                    {
                        mqttDataDAL.UpdateMqttData(mqttdata);
                    }
                    else
                    {
                        mqttDataDAL.AddMqttData(mqttdata);
                    }
                }
                catch
                {

                }
            }

            try
            {
                //更新阈值告警
                mqttDataDAL.UpdateElevatorBend();
                bool result_7 = PushPromptMessage("RefreshPrompt").Result;
            }
            catch
            {

            }
        }

        public static async Task<bool> PushPromptMessage(string message)
        {
            return await ElevatorWebSocketMiddleware.webSocketManage.PushPromptMessage(message);
        }
    }
}

